package com.xiaofu.normal;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.xiaofu.utils.ConnectUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author luofuzhou
 * @date 2024/11/19
 * @dec 描述
 */
public class RabbitConsumer1 {
    private final static String QUEUE_NAME = "xiaofu";
    public static void main(String[] args) throws Exception {
        Channel channel = ConnectUtil.getChannel();

        channel.basicQos(0);

        channel.basicConsume(QUEUE_NAME, false, new DeliverCallback() {

            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                System.out.println(new String(body));
                try {
                    TimeUnit.SECONDS.sleep(4);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            }
        }, new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("消息被中断");
            }
        });

    }
}
